<template>
  <div @click="$router.push('/article/'+artId)">
    <van-cell>
      <!-- 标题区域的插槽 -->
      <template #title>
        <div class="title-box">
          <!-- 标题 -->
          <span>{{article.title}}</span>
          <!-- 单张图片 -->
          <!-- 图片懒加载，把src设成v-lazy -->
          <img v-lazy="article.cover.images[0]" alt="" class="thumb" v-if="article.cover.type ===1">
        </div>
        <!-- 三张图片 -->
        <div class="thumb-box" v-if="article.cover.type ===3" >
          <img v-lazy="item" alt="" v-for="(item, index) in article.cover.images" :key="index" class="thumb">
        </div>
      </template>
      <!-- label 区域的插槽 -->
      <template #label>
        <div class="label-box">
          <span>{{article.aut_name}} &nbsp;&nbsp; {{article.comm_count}}评论 &nbsp;&nbsp; {{article.pubdate | dateFormat}}</span>
          <!-- 关闭按钮 -->
          <van-icon name="cross" @click.stop="show=true" v-if="closeable"/>
        </div>
      </template>
    </van-cell>

    <!-- 动作面板 -->
    <van-action-sheet v-model="show" cancel-text="取消" :closeable="false" @closed="isFirst=true" get-container="body">
      <!-- 一级面板 -->
      <div v-if="isFirst">
    <van-cell :title="item.name" class="center-title" clickable v-for="item in actions" :key="item.name" @click="onCellClick(item.name)"/>
    </div>
    <!-- 二级面板 -->
    <div v-else>
      <van-cell title="返回" class="center-title" clickable @click="isFirst=true"/>
      <!-- 循环二级数组的数据 -->
      <van-cell :title="item.label" class="center-title" clickable v-for="item in reports" :key="item.type" @click="reportArticle(item.type)"/>
    </div>
    </van-action-sheet>
  </div>
</template>


<script>
// 注意，如果某个数据，在组件模板渲染期间，相使用数据，要转存到data中
import reports from '@/api/reports.js'
import { dislikeArticleApi, reportArticleApi } from '@/api/homeApi.js'
export default {
  name: 'ArtItem',
  props: {
    // 文章信息对象
    article: {
      type: Object, // 数据类型
      required: true// 必填项
    },
    // 是否展示图标
    closeable: {
      type: Boolean,
      default: true
    }
  },
  data () {
    return {
      // 控制动作面板的显示与隐藏
      show: false,
      // 第一个面板的可选项列表
    actions: [
      { name: '不感兴趣' },
      { name: '反馈垃圾内容' },
      { name: '拉黑作者' }
    ],
    // 一二级面板切换
    isFirst: true,
    reports
    }
  },
  methods: {
    // 点击了一级选项
   async onCellClick (name) {
      if (name === '不感兴趣') {
       const { data: res } = await dislikeArticleApi(this.artId)
       if (res.message === 'OK') {
        //  TODO:炸楼操作,把这篇文章从列表中删除

        this.$emit('remove-artlist', this.artId)
       }
        this.show = false
      } else if (name === '拉黑作者') {
        console.log('拉黑作者')
        this.show = false
      } else if (name === '反馈垃圾内容') {
        // 展示二级内容
        this.isFirst = false
      }
    },
    // 举报文章
   async reportArticle (type) {
     try {
      //  掉接口
         await reportArticleApi(this.artId, type)
     } catch {
      //  掉接口失败
       this.$toast.fail('此文章已被举报')
     }
     // 不论成功失败，炸楼操作
       this.$emit('remove-artlist', this.artId)
      //  关闭动作面板
       this.show = false
    }
  },
  computed: {
    artId () {
      // 返回文章id
      // 调用toString方法，把大数对象，转为字符串格式数字
      return this.article.art_id.toString()
    }
  }
}
</script>

<style lang='less' scoped>
.label-box {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.thumb {
  // 矩形黄金比例：0.618
  width: 113px;
  height: 70px;
  background-color: #f8f8f8;
  object-fit: cover;
}

.title-box {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
}

.thumb-box {
  display: flex;
  justify-content: space-between;
}
.center-title{
  text-align: center;
}
</style>
